package org.desktop.diskutils.components.treemap.actions;

import org.desktop.diskutils.business.categories.IFileCategory;
import org.desktop.diskutils.business.files.IFileTreeNode;
import org.desktop.diskutils.components.treemap.TreeBuilder;

import prefuse.Visualization;
import prefuse.action.assignment.ColorAction;
import prefuse.util.ColorLib;
import prefuse.util.ColorMap;
import prefuse.visual.NodeItem;
import prefuse.visual.VisualItem;

/**
 * Set fill colors for treemap nodes. Search items are colored in pink, while
 * normal nodes are shaded according to their depth in the tree.
 */
public class FillColorAction extends ColorAction {
	private static final int LEAF_COLOR_ALPHA = 200;

	private ColorMap cmap = new ColorMap(ColorLib.getInterpolatedPalette(10, ColorLib.rgb(85, 85, 85),
			ColorLib.rgb(0, 0, 0)), 0, 9);


	public FillColorAction(final String group) {
		super(group, VisualItem.FILLCOLOR);
	}

	public int getColor(final VisualItem item) {
		if (item instanceof NodeItem) {
			NodeItem nitem = (NodeItem) item;
			if (nitem.getChildCount() > 0) {
				return 0; // no fill for parent nodes
			} else {
				if (m_vis.isInGroup(item, Visualization.SEARCH_ITEMS)) {
					return ColorLib.rgb(191, 99, 130);
				} else {
					IFileCategory category = ((IFileTreeNode)nitem.get(TreeBuilder.BUSINESS_TREE_NODE_ATTR)).getTree().getRootCategory();
					String categoryName = nitem.getString(TreeBuilder.CATEGORY_NAME_ATTR);
					int color = category.getEntryFor(categoryName).getColor();
					return ColorLib.setAlpha(color, LEAF_COLOR_ALPHA);
				}
			}
		} else {
			return cmap.getColor(0);
		}
	}

}
